// index.js
const { Toast, Dialog } = require("../../config/modal")
const utils = require("../../utils/util")
const config = require("../../config/config")
const { LOGIN, LOCATION } = require("../../config/login.js")
// 获取应用实例
const App = getApp()
Page({
    data: {
        title: '去抖视频水印',
        host: '',
        offsetTop: App.globalData.window.navHeight,
        capsuleBarHeight: App.$getMenuButtonHeight(),
        isParse: true,
        test: "https://aweme.snssdk.com/aweme/v1/play/?video_id=v0300fg10000c4im2drc77u6qjgms3ig&ratio=720p&line=0",
        video: {
            src: '',
            desc: "视频文案",
            audioApi: ""
        },
        images: [{ name: "抖音", src: "/static/img/logo-douyin.png" }, { name: "快手", src: "/static/img/logo-kuaishou.png" }, { name: "微视", src: "/static/img/logo-weishi.png" }, { name: "火山", src: "/static/img/logo-huoshan.png" }],
        value: '',//https://v.douyin.com/d8cCTmB/,

        // 主题颜色
        theamTextColor: App.globalData.theamTextColor,

        copyData: {},

        // 通知条
        notice: "在代码阅读过程中人们说脏话的频率是衡量代码质量的唯一标准。",
    },
    page: {

    },
    URL: {
        douYin: "/parse/douyin",
    },

    onLoad(options) {

    },
    copyAudioLink() {
        if (utils.isInvalid(this.data.video.audioApi)) {
            Toast("未检测到音频");
            return;
        }
        let that = this
        wx.setClipboardData({
            data: this.data.video.audioApi,
            success(res) {
                wx.getClipboardData({
                    success(res) {
                        console.log(res.data) // data
                    }
                })
            }
        })

    },
    clearCopyData() {
        this.setData({
            value: ""
        })
    },
    onInput(e) {
        let that = this
        wx.getClipboardData({
            success(res) {
                that.setData({
                    value: e.detail.value
                });
            }
        })
    },
    copyDesc() {
        let that = this
        wx.setClipboardData({
            data: that.data.video.desc,
            success(res) {
                wx.getClipboardData({
                    success(res) {
                        console.log(res.data) // data
                    }
                })
            }
        })
    },
    copyData() {
        let that = this
        wx.getClipboardData({
            success(res) {
                that.setData({
                    value: res.data
                });
            }
        })
    },
    onSearch() {
        if (this.data.value == '' || this.data.value == undefined) {
            Toast("未检测到视频链接");
            return;
        }

        if (this.data.value.indexOf("http") < 0) {
            Toast("未检测到内容存在http视频链接");
            return;
        }
        wx.pageScrollTo({
            scrollTop: 10000,
        })
        App.$post(true, this.URL.douYin, { url: this.data.value }, res => {
            if (res.R) {
                this.setData({
                    'video.src': res.data.playApi,
                    'video.desc': res.data.desc,
                    'video.audioApi': res.data.audioApi,
                    isParse: true
                })
                return;
            }
        })
    },
    audioPlayError(e) {
        debugger
        Toast("音频播放失败");
    },

    onReady() {
        this.audioCtx = wx.createAudioContext('myAudio')
    },

    audioPlay: function () {
        this.audioCtx.play()
    },
    audioPause: function () {
        this.audioCtx.pause()
    },

    onDownVideo() {
        let that = this;

        if (this.data.video.src == '' || this.data.video.src == undefined) {
            Toast("未解析到视频");
            return;
        }

        let poster = this.data.video.src

        wx.showLoading({
            title: '视频保存中',
            mask: true,
        })
        wx.downloadFile({
            url: that.data.host + "/parse/downVideo?url=" + poster,
            filePath: wx.env.USER_DATA_PATH + '/videodouyin.mp4',
            fail(res) {
            },
            complete(res) {
                console.log(res);
                wx.hideLoading({})
            },
            success(res) {
                console.log(res)
                if (res.statusCode == 200) {
                    wx.saveVideoToPhotosAlbum({
                        filePath: res.filePath,
                        success: function (res) {
                            wx.hideLoading({})
                            Toast('视频已保存到相册中')
                        },
                        fail: function (err) {
                            Toast('视频已保存失败');
                            wx.hideLoading({})
                        }
                    })
                }
            }

        });

    },

    onShow() {

        let server = config.server;
        let activeServer = server[config.active];
        this.setData({
            host: activeServer.doMain
        })

        let that = this
        wx.getClipboardData({
            success(res) {
                let cpData = that.data.copyData;
                if (utils.isInvalid(cpData[res.data])) {
                    if (res.data.indexOf("http") >= 0 && !utils.isInvalid(res.data)) {
                        cpData[res.data] = true;
                        that.setData({
                            value: res.data,
                            copyData: cpData
                        });
                        Toast('链接已粘贴，点击开始去除水印')
                    }
                }
            }
        })
    },

    // tab栏切换
    onTypeChange(event) {


    },






    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {
        wx.stopPullDownRefresh();
    },



    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },
    onShareAppMessage: function () {
        return App.$onShare('/pages/index/index', null, "抖音去水印")
    }

})
